/** @Date 下午6:25:55
  * @version 1.0.0
  * @author shangyd
  * Copyright (c) 2015
  */
package com.sdw.soft.test.zookeeper;

import java.io.IOException;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ZookeeperDemo {

	private static final Logger logger = LoggerFactory.getLogger(ZookeeperDemo.class);
	
	private static final int SESSIONTIMEOUT = 3000;
	private static final String HOST = "192.168.183.133:2181";
	private static ZooKeeper zk = null;
	@Before
	public void test0(){
		try {
			zk = new ZooKeeper(HOST, SESSIONTIMEOUT, new Watcher() {
				@Override
				public void process(WatchedEvent event) {
					logger.info("watcher event say:" + event.getState());
					
					if(event.getType() == EventType.NodeDeleted){
						logger.info("node delete");
					}else if(event.getType() == EventType.NodeChildrenChanged){
						logger.info("node NodeChildrenChanged");
					}else if(event.getType() == EventType.NodeCreated){
						logger.info("node NodeCreated");
					}else if(event.getType() == EventType.NodeDataChanged){
						logger.info("node NodeDataChanged");
					}
				}
			});
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * create znode
	 * @throws Exception
	 */
	@Test
	public void test01() throws Exception {
		zk.create("/node", "/data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	}
	/**
	 * delete znode
	 */
	@Test
	public void test02(){
		try {
			zk.delete("/node", -1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (KeeperException e) {
			e.printStackTrace();
		}
	}
	/**
	 * set get data
	 */
	@Test
	public void test03(){
		try {
			zk.create("/node", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			zk.setData("/node", "/data1".getBytes(), -1);
			Stat stat = new Stat();
			byte[] data = zk.getData("/node", true, stat);
			logger.info("data="+new String(data));
		} catch (KeeperException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * add children node
	 */
	@Test
	public void test04(){
		try {
			zk.create("/node/children", "children ndoe".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		} catch (KeeperException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * adjust the node exists
	 */
	@Test
	public void test05(){
		try {
			Stat stat = zk.exists("/node/children", true);
			if(stat == null){
				logger.info("节点不存在!");
			}else{
				logger.info("节点存在!");
			}
		} catch (KeeperException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	

	@After
	public void afterTest(){
		try {
			zk.close();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
